//
//  TabBarViewController.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/14.
//
//

#import "TabBarViewController.h"
#import "TabBar.h"
#import "ImageTitleButton.h"
#import "Home.h"
#import "Mine.h"
#import "Order.h"
#import "Login.h"
#import "DataFrameParam.h"

#define BTN_HEIGHT 50
#define BTNTAG 10000
@interface TabBarViewController ()<UITabBarControllerDelegate>
{
    UIButton *_button;
    UIButton *_butTitle;
}
@property (nonatomic,strong)UIButton *b1;
@property (nonatomic,strong)UIButton *b2;
@property (nonatomic,strong)UIButton *b3;
@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
    [self setValue:[[TabBar alloc] init] forKey:@"tabBar"];
    if (@available(iOS 15.0, *))  {
        UITabBarAppearance *barApp = [[UITabBarAppearance alloc] init];
        barApp.backgroundColor = [UIColor clearColor];
        barApp.shadowColor = UIColor.clearColor;
        self.tabBar.standardAppearance = barApp;
        self.tabBar.scrollEdgeAppearance = barApp;
    } else {
        [self.tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]];
        [self.tabBar setShadowImage:[UIImage imageWithColor:UIColor.clearColor]];
    }
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[UIImage new]]; // 移除顶部分隔线
    
    self.tabBar.backgroundColor = [UIColor clearColor];
    
    if (@available(iOS 15.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithTransparentBackground];
        appearance.backgroundColor = [UIColor clearColor];
        
        self.tabBar.standardAppearance = appearance;
        self.tabBar.scrollEdgeAppearance = appearance;
    }

    self.view.backgroundColor = [UIColor clearColor];
    [self initControllers];
    [self creatTabbar];
}

- (void)initControllers
{
    Home *homeVC  = [[Home alloc]init];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homeVC];
    Order * orderVC = [[Order alloc]init];
    UINavigationController *orderVCNav = [[UINavigationController alloc]initWithRootViewController:orderVC];
    Mine * uerVC = [[Mine alloc]init];
    UINavigationController *mineNav = [[UINavigationController alloc]initWithRootViewController:uerVC];
    NSArray *ctrlArr = [NSArray arrayWithObjects:homeNav,orderVCNav,mineNav,nil];
    self.viewControllers=ctrlArr;
}
-(void)selectIndex:(NSInteger)index{
    _butTitle.selected = _button.selected = NO;
    _butTitle.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *tmpImgBtn = [self.view viewWithTag:10000 + index+10];
    UIButton *tmpTitleBtn = [self.view viewWithTag:10000 + index+20];
    tmpTitleBtn.selected = tmpImgBtn.selected = YES;
    _butTitle = tmpTitleBtn;
    _button = tmpImgBtn;
    tmpTitleBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
    self.selectedIndex = index;
}

- (void)creatTabbar{
    NSArray *titleArrs = @[@"Home",@"Order",@"My"];
    NSArray *imgArrs = @[@"home",@"order",@"me"];
    NSArray *simgArrs = @[@"home_s",@"order_s",@"me_s"];
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, self.tabBar.frame.size.height-bottomSafeHeight)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.layer.cornerRadius = 38;
    bgView.userInteractionEnabled = YES;
    float btn_X = 0;
    float btn_width = (SCREEN_WIDTH - 20)/self.viewControllers.count;
    if ([self isIpad]) {
        btn_X = 100;
        btn_width = (SCREEN_WIDTH-btn_X*2-20)/self.viewControllers.count;
    }
    for(int i = 0;i<self.viewControllers.count;i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(btn_X+btn_width*i, 0, btn_width, self.tabBar.frame.size.height);
        [bgView addSubview:btn];
        ImageTitleButton *button = [[ImageTitleButton alloc] initWithFrame:CGRectMake(btn_X+btn_width*i, -20, btn_width, self.tabBar.frame.size.height)];
        [button setTitle:titleArrs[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button setImage:[UIImage imageNamed:imgArrs[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:simgArrs[i]] forState:UIControlStateSelected];
        [button setTitleColor:DetailColor forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [bgView addSubview:button];
        UIButton *chooseBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        chooseBtn.frame = CGRectMake(btn_X+btn_width*i, 0, btn_width, 54);
        [chooseBtn addTarget:self action:@selector(btnSelect:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:chooseBtn];
        if (i == 0) {
            _button = btn;
            _butTitle = button;
            button.titleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightBold];
            button.selected = btn.selected = YES;
            [button setTitleColor:MainColor forState:UIControlStateNormal];
        }
        chooseBtn.tag = BTNTAG+i;
        btn.tag = BTNTAG+i+10;
        button.tag = BTNTAG+i+20;
        if(i == 0){
            _b1 =button;
        }
        if(i == 1){
            _b2 =button;
        }
        if(i == 2){
            _b3 =button;
        }
    }
    [self.tabBar addSubview:bgView];
}
- (NSString *)stewardess {
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if(stewardess != nil){
        return stewardess;
    } else {
        return @"";
    }
}

- (void)btnSelect:(UIButton *)sender{
    NSString *t = [self stewardess];
    if ([t isEqualToString:@""]&&sender.tag!=BTNTAG) {
        Login *login = [Login new];
        [self presentViewController:login animated:YES completion:nil];
    }
    _butTitle.selected = _button.selected = NO;
    _butTitle.titleLabel.font = [UIFont systemFontOfSize:10];
    [_butTitle setTitleColor:DetailColor forState:UIControlStateNormal];
    UIButton *tmpImgBtn = [self.view viewWithTag:sender.tag+10];
    UIButton *tmpTitleBtn = [self.view viewWithTag:sender.tag+20];
    tmpTitleBtn.selected = tmpImgBtn.selected = YES;
    _butTitle = tmpTitleBtn;
    _button = tmpImgBtn;
    tmpTitleBtn.titleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightBold];
    [tmpTitleBtn setTitleColor:MainColor forState:UIControlStateNormal];
    self.selectedIndex = sender.tag-BTNTAG;
}


- (BOOL)isIpad{
    NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType isEqualToString:@"iPhone"]){
        return NO;
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        return NO;
    }
    else if([deviceType isEqualToString:@"iPad"]) {
        return YES;
    }
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
